
import re
import os

# 要统计的目录
inputPath = input("请输入要统计的目录路径：")
if inputPath == '' :
	inputPath = './'

# 是否递归统计目录下文件夹中的文件
inputIsRec = input("是否递归统计目录下文件夹中的文件（Y/N）?：")

def printPath(dirLevel, pathType, nowPath, currCount) :
	if dirLevel > 0 :
		if pathType == 'dir' :
			print(" │  "*dirLevel, "├──", nowPath, ":", currCount)
		else :
			print(" │  "*dirLevel, "└──", nowPath, ":", currCount)
	else :
		if pathType == 'dir' :
			print("│  "*dirLevel, "├──", nowPath, ":", currCount)
		else :
			print("│  "*dirLevel, "└──", nowPath, ":", currCount)



def countRows(path, dirLevel, *regExp) :
	regExpLen = len(regExp)
	if regExpLen == 0 :
		#不传正则表达式
		count = 0
		currFileCount = 0
		paths = os.listdir(path)
		for nowPath in paths:
			absNowPath = path + "\\" + nowPath
			if inputIsRec == 'Y' or inputIsRec == 'y' or inputIsRec == '' :
				if os.path.isdir(absNowPath) :
					printPath(dirLevel, "dir", "◸ " + nowPath, "开始计算")
					currDirCount = countRows(absNowPath, dirLevel+1)
					count += currDirCount
					printPath(dirLevel, "dir", "◺ " + nowPath, currDirCount)
				elif os.path.isfile(absNowPath) :
					openFile = open(absNowPath, 'rb')
					for index, line in enumerate(openFile):
						count += 1
						currFileCount += 1
					openFile.close()
					printPath(dirLevel, "file", nowPath, currFileCount)
					currFileCount = 0
			else :
				if os.path.isfile(absNowPath) :
					openFile = open(absNowPath,'rb')
					for index, line in enumerate(openFile):
						count += 1
						currFileCount += 1
					openFile.close()
					printPath(dirLevel, "file", nowPath, currFileCount)
					currFileCount = 0
		return count
	elif regExpLen == 1 :
		print('一个正则参数，只统计符合该正则的文件，暂未开发')
		return 0
	else:
		print('两个正则参数，第一个正则匹配文件，第二个正则匹配文件夹，暂未开发')
		return 0

print(inputPath)
print("总行数:", countRows(inputPath, 0)) 